package com.rentong.utils;

import cn.hutool.core.collection.CollectionUtil;
import com.rentong.wx.cust.dao.*;
import com.rentong.wx.cust.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

@Component
@Order(value = 2)
public class HalfMinuteTimer implements CommandLineRunner {
    private static final long ORDER_TIME = 30 * 1000;

    @Autowired
    private CustServiceOrderChefDao custServiceOrderChefDao;

    @Autowired
    private CustServiceOrderDao custServiceOrderDao;

    @Autowired
    private WxCustUserMapper wxCustUserMapper;

    @Autowired
    private CustFlowDao custFlowDao;

    @Autowired
    private CustFlowTypeDao custFlowTypeDao;

    @Override
    public void run(String... args) throws Exception {
        Calendar date = Calendar.getInstance();
        //设置时间为 xx-xx-xx 00:00:00
        date.set(date.get(Calendar.YEAR), date.get(Calendar.MONTH), date.get(Calendar.DATE),
                0, 0, 12);

        //得到定时器实例
        Timer t1 = new Timer();
        Timer t2 = new Timer();

        //30分钟后，用户未操作选择该厨师，将状态变成拒绝
        t1.schedule(new TimerTask() {
            public void run() {
                List<CustServiceOrderChefEntity> custServiceOrderChefEntities = custServiceOrderChefDao.getTimeoutChef();
                if (CollectionUtil.isNotEmpty(custServiceOrderChefEntities)) {
                    for (CustServiceOrderChefEntity custServiceOrderChefEntity : custServiceOrderChefEntities) {
                        custServiceOrderChefEntity.setServiceOrderChefState(3);
                        custServiceOrderChefEntity.setUpdateTime(new Date());
                        custServiceOrderChefDao.updateByPrimaryKeySelective(custServiceOrderChefEntity);
                    }
                }
            }
        }, date.getTime(), ORDER_TIME); //daySpan是一天的毫秒数，也是执行间隔


        //48小时后自动确认
        t2.schedule(new TimerTask() {
            public void run() {
                List<CustServiceOrderEntity> custServiceOrderEntities = custServiceOrderDao.getCompleteOrder();
                if (CollectionUtil.isNotEmpty(custServiceOrderEntities)) {
                    for (CustServiceOrderEntity custServiceOrderEntity : custServiceOrderEntities) {
                        //更新状态
                        Long serviceOrderId = custServiceOrderEntity.getServiceOrderId();
                        CustServiceOrderEntity custServiceOrder = new CustServiceOrderEntity();
                        custServiceOrder.setOrderState(6);
                        custServiceOrder.setServiceOrderId(serviceOrderId);
                        custServiceOrder.setUpdateTime(new Date());
                        custServiceOrderDao.updateByPrimaryKeySelective(custServiceOrder);

                        //更新接单师傅余额
                        BigDecimal amount = custServiceOrderEntity.getTrueAmount();
                        amount = amount.multiply(BigDecimal.ONE.subtract(custServiceOrderEntity.getPingtaiBili().divide(BigDecimal.valueOf(10000))));
                        wxCustUserMapper.updateAmount(amount, custServiceOrderEntity.getChefId());


                        CustFlowTypeEntity cfte2 = custFlowTypeDao.selectByPrimaryKey(2L);
                        WxCustUser wc2 = wxCustUserMapper.selectByPrimaryKey(custServiceOrderEntity.getChefId());
                        //插入cust_flow
                        CustFlowEntity custFlowEntity = new CustFlowEntity();
                        custFlowEntity.setCustId(custServiceOrderEntity.getChefId());
                        custFlowEntity.setType(cfte2.getType());
                        custFlowEntity.setFlowTypeName(cfte2.getFlowTypeName());
                        custFlowEntity.setFlowTypeId(cfte2.getFlowTypeId());
                        custFlowEntity.setFee(amount);
                        custFlowEntity.setFeeBefore(wc2.getBalanceAmount());
                        custFlowEntity.setFeeAfter(wc2.getBalanceAmount().add(amount));
                        custFlowEntity.setAddTime(new Date());
                        custFlowEntity.setServiceOrderId(serviceOrderId);
                        custFlowDao.insert(custFlowEntity);


                        //分享赚
                        if (custServiceOrderEntity.getShareCustId() != 0) {
                            BigDecimal shareAmount = custServiceOrderEntity.getTrueAmount().subtract(amount).multiply(custServiceOrderEntity.getShareBili().divide(BigDecimal.valueOf(10000)));
                            wxCustUserMapper.updateAmount(shareAmount, custServiceOrderEntity.getCustId());

                            CustFlowTypeEntity cfte1 = custFlowTypeDao.selectByPrimaryKey(3L);
                            WxCustUser wc1 = wxCustUserMapper.selectByPrimaryKey(custServiceOrderEntity.getShareCustId());
                            //收支明细
                            CustFlowEntity cfe = new CustFlowEntity();
                            cfe.setCustId(custServiceOrderEntity.getShareCustId());
                            cfe.setType(cfte1.getType());
                            cfe.setFlowTypeName(cfte1.getFlowTypeName());
                            cfe.setFlowTypeId(cfte1.getFlowTypeId());
                            cfe.setFee(shareAmount);
                            cfe.setFeeBefore(wc1.getBalanceAmount());
                            cfe.setFeeAfter(wc1.getBalanceAmount().add(amount));
                            cfe.setServiceOrderId(serviceOrderId);
                            cfe.setAddTime(new Date());
                            custFlowDao.insert(cfe);
                        }


                        //查询下，是否是新的店铺
                        Integer num = custServiceOrderDao.getNumByCustId(custServiceOrderEntity.getCustId(), custServiceOrderEntity.getChefId());
                        WxCustUser custUser = wxCustUserMapper.selectByPrimaryKey(custServiceOrderEntity.getChefId());

                        //更新接单总量和合作店铺
                        WxCustUser wc = new WxCustUser();
                        wc.setCustId(custServiceOrderEntity.getChefId());
                        wc.setTodayAmount(custUser.getTodayAmount().add(amount));
                        wc.setJiedanNum(custUser.getJiedanNum() + 1);
                        wc.setHezuoNum(custUser.getHezuoNum() + (num == 1 ? num : 0));
                        wc.setUpdateTime(new Date());
                        wxCustUserMapper.updateByPrimaryKeySelective(wc);

                    }
                }
            }
        }, date.getTime(), ORDER_TIME); //daySpan是一天的毫秒数，也是执行间隔

    }

}
